'use strict'

const Service = require('egg').Service;

class LoginService extends Service{
    async getUser(username, password,rid){
        var user = await this.app.mysql.get('manager',{phone:username});
        const restaurant = await this.app.mysql.get('restaurant',{id:rid});
        if(user && rid){
            if(user.password === password){
                return {
                    uid:user.id,
                    rid:restaurant.id
                }
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    async getRestaurant(username){
        try{
            var user = await this.app.mysql.get('manager',{phone:username});
            const rids = await this.app.mysql.select('manager_restaurant',{
                where:{mid:user.id},
                columns:['rid']
            })
            var ridList = [];
            for (var i=0; i!=rids.length; ++i){
                ridList.push(rids[i].rid);
            }

            const restaurant =await this.app.mysql.select('restaurant',{
                where:{id:ridList}
            });
            return restaurant;

        }catch(err){
            this.ctx.status = 500;
        }
    }
 
}
module.exports = LoginService
